clc; close all; clear;
addpath('../02_Functions');

%------------------------------------------------------------------------%
% This file creates figure 8(c-d) in the paper 
%------------------------------------------------------------------------%

data = load('../01_Data/es_trades_5minute_EST.mat');

No_trades                                                                       = data.trades_per_min_mat;
No_contracts                                                                    = data.trade_quantity_per_min_mat;

% choose sample period ---------------------------------------------------------------------------------- %

start_date                  = datenum(2019,12,27);
finish_date                 = datenum(2020,12,29);

beg                         = find_closest(data.caldt_daily,start_date);
fin                         = find_closest(data.caldt_daily,finish_date);

caldt                       = data.caldt_daily(beg:fin);
No_trades                   = No_trades(beg:fin,:); 
Volume                      = No_contracts(beg:fin,:); 

fprintf(1,'********************************\n')
fprintf(1,'Sample: Emini          \n')
fprintf('begin %12s  \n',datestr(caldt(1)))
fprintf('finish %12s  \n',datestr(caldt(end)))
fprintf(1,'********************************\n')

% --------------------------------------------------------------------------------------

ADIndex                     = 1 : 288;              % 18:10 - 18:00
ONIndex                     = 1 : 186;              % 18:10 - 9:30
IDIndex                     = 186 : 266;            % 9:30 - 16:15

% ON ID VOLUME TIME SERIES 
No_trades_ON                = sum(No_trades(:,ONIndex),2);    
No_trades_ID                = sum(No_trades(:,IDIndex),2);  
Volume_ON                   = sum(Volume(:,ONIndex),2);  
Volume_ID                   = sum(Volume(:,IDIndex),2);  


caldt                       = datetime(caldt,'ConvertFrom','datenum');
TT                          = timetable(caldt,Volume_ON,Volume_ID,'VariableNames',{'volumeON','volumeID'});
TT                          = retime(TT,'monthly','mean');
TT.caldt                    = dateshift(TT.caldt,'end','month');
Volume_ON_monthly           = TT.volumeON;
Volume_ID_monthly           = TT.volumeID;
caldt_monthly               = TT.caldt;
caldt_monthly               = datenum(caldt_monthly);

% --------------------------------------------------------------------------------------

% HOURLY AVERAGES 
ON_No_trades                = No_trades(:,ONIndex);
ID_No_trades                = No_trades(:,IDIndex); 
ON_Volume                   = Volume(:,ONIndex);
ID_Volume                   = Volume(:,IDIndex); 

% --------------------------------------------------------------------------------------

window                      = 21;
TT                          = length(ON_No_trades);

%------------------------------------------------------------------------%

%%%%% ALL DAY %%%%%

TTT                     = length(Volume);
mu_Volume               = nanmean(Volume,1);
var_Volume              = var(Volume,1,1);
se_Volume               = sqrt(var_Volume/TTT);


hour_index              = 0 : 1 : 288;
x_ticks                 = 0 : 12 : 288;
hour_string             = {'18' , '19' , '20' , '21' , '22' , '23' , '24' , '01' , '02' , '03', '04' , '05' , '06' , '07' , '08' , '9' , '10' , '11' , '12' , '13' , '14' , '15' , '16' , '17' , '18'};

mu_Volume_plot          = [nan mu_Volume];

figure;
bar(hour_index , mu_Volume_plot);
hold on
ylim([0 max(mu_Volume_plot)*1.1])
xlim([0 288])
ylabel('Average Volume Traded' )
xlabel('Trade Time (ET)' )
% ylabel('return (% pa)' )
set(gca,'XTick',x_ticks,'XTickLabel',hour_string);
legend off



%------------------------------------------------------------------------%
%------------------------------------------------------------------------%

asia_start = 1;
asia_end   = 96;
EU_start   = 97;
EU_end     = 156;
OD_start   = 97;
OD_end     = 108;
CTC_start  = 1;
CTC_end    = 267;

ONIndex    = asia_start:EU_end;
Volume     = Volume(:,ONIndex);

%%%%% OVERNIGHT %%%%%

TTT             = length(Volume);
mu_Volume       = nanmean(Volume,1);
var_Volume      = var(Volume,1,1,'omitnan');
se_Volume       = sqrt(var_Volume/TTT);


hour_index      = 0 : 1  : EU_end;
x_ticks         = 0 : 12 : EU_end;
hour_string     = {'18' , '19' , '20' , '21' , '22' , '23' , '24' , '01' , '02' , '03', '04', '05', '06', '07'};

mu_Volume_plot = [nan mu_Volume];

figure;
bar(hour_index , mu_Volume_plot);
hold on
ylim([0 max(mu_Volume_plot)*1.1])
xlim([0 EU_end])
ylabel('Average Volume Traded' )
xlabel('Trade Time (ET)' )
% ylabel('return (% pa)' )
set(gca,'XTick',x_ticks,'XTickLabel',hour_string);
legend off

